﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Threading;
using System.Threading.Tasks.Dataflow;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public class Test_CallbackSubscription
	{
		private class TestMessage : Message
		{
		}

		private class TestInformation : Information
		{
		}


		Int32 mSubscriberCallbackCalled;
		Int32 mRemoveSubscriptionCalled;
		SubscriptionBase mRemoveSubscriptionArgument;

		private void _SubscriberCallback(Message message)
		{
			SpinWait.SpinUntil(() => false, 500);
			Interlocked.Increment(ref mSubscriberCallbackCalled);
		}

		private void _RemoveSubscription(SubscriptionBase subscription)
		{
			mRemoveSubscriptionArgument = subscription;
			Interlocked.Increment(ref mRemoveSubscriptionCalled);
		}

		private void _Reset()
		{
			mSubscriberCallbackCalled = 0;
			mRemoveSubscriptionCalled = 0;
			mRemoveSubscriptionArgument = null;
		}

		private BroadcastBlock<Message> _CreateBroadcastBlock()
		{
			var block = new BroadcastBlock<Message>(
				null);

			block.Post(new NullMessage());

			return block;
		}


		// --- TESTS ---


		[Test]
		public void Test_Complete_DiscardAvailableMessages()
		{
			_Reset();

			// setup
			var subscription = new CallbackSubscription(
				_SubscriberCallback,
				typeof(Message),
				null,
				_RemoveSubscription);
			var subscriptionInterface = (ICompletable)subscription;

			var broadcastBlock = _CreateBroadcastBlock();
			subscription.Link(broadcastBlock);

			// post
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());

			Helper.WaitForCondition(5000, 100, () => mSubscriberCallbackCalled == 2);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(2));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(0));

			// complete
			subscriptionInterface.Complete(discardAvailableMessages: true);
			subscriptionInterface.Completion.Wait();

			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(3));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));

			// post after completion
			broadcastBlock.Post(new TestMessage());

			Helper.WaitForCondition(1000, 100, () => mSubscriberCallbackCalled == 4);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(3));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));

			// complete again
			subscriptionInterface.Complete(discardAvailableMessages: true);
			subscriptionInterface.Completion.Wait();

			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(3));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));
		}

		[Test]
		public void Test_Complete_ProcessAvailableMessages()
		{
			_Reset();

			// setup
			var subscription = new CallbackSubscription(
				_SubscriberCallback,
				typeof(Message),
				null,
				_RemoveSubscription);
			var subscriptionInterface = (ICompletable)subscription;

			var broadcastBlock = _CreateBroadcastBlock();
			subscription.Link(broadcastBlock);

			// post
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());

			Helper.WaitForCondition(5000, 100, () => mSubscriberCallbackCalled == 2);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(2));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(0));

			// complete
			subscriptionInterface.Complete(discardAvailableMessages: false);
			subscriptionInterface.Completion.Wait();

			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(4));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));

			// post after completion
			broadcastBlock.Post(new TestMessage());

			Helper.WaitForCondition(1000, 100, () => mSubscriberCallbackCalled == 5);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(4));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));

			// complete again
			subscriptionInterface.Complete(discardAvailableMessages: true);
			subscriptionInterface.Completion.Wait();

			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(4));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));
		}

		[Test]
		public void Test_Dispose()
		{
			_Reset();

			// setup
			var subscription = new CallbackSubscription(
				_SubscriberCallback,
				typeof(Message),
				null,
				_RemoveSubscription);

			var broadcastBlock = _CreateBroadcastBlock();
			subscription.Link(broadcastBlock);

			// post
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());

			Helper.WaitForCondition(5000, 100, () => mSubscriberCallbackCalled == 2);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(2));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(0));

			// dispose
			Helper.Dispose(subscription);

			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(3));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));

			// post after completion
			broadcastBlock.Post(new TestMessage());

			Helper.WaitForCondition(1000, 100, () => mSubscriberCallbackCalled == 4);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(3));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));

			// dispose again
			Helper.Dispose(subscription);

			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(3));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));
		}

		[Test]
		public void Test_Multiple_Concurrent_Complete()
		{
			_Reset();

			// setup
			var subscription = new CallbackSubscription(
				_SubscriberCallback,
				typeof(Message),
				null,
				_RemoveSubscription);
			var subscriptionInterface = (ICompletable)subscription;

			var broadcastBlock = _CreateBroadcastBlock();
			subscription.Link(broadcastBlock);

			// post
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestMessage());

			Helper.WaitForCondition(5000, 100, () => mSubscriberCallbackCalled == 2);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(2));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(0));

			// complete
			TaskHelper.Run(() => subscriptionInterface.Complete(true));
			TaskHelper.Run(() => subscriptionInterface.Complete(true));
			TaskHelper.Run(() => subscriptionInterface.Complete(true));
			TaskHelper.Run(() => subscriptionInterface.Complete(true));
			subscriptionInterface.Completion.Wait();

			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(3));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));

			// post after completion
			broadcastBlock.Post(new TestMessage());

			Helper.WaitForCondition(1000, 100, () => mSubscriberCallbackCalled == 4);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(3));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));

			// complete again
			subscriptionInterface.Complete(discardAvailableMessages: true);
			subscriptionInterface.Completion.Wait();

			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(3));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionArgument, Is.SameAs(subscription));
		}

		[Test]
		public void Test_MessageFilter1()
		{
			_Reset();

			// setup
			var subscription = new CallbackSubscription(
				_SubscriberCallback,
				typeof(Message),
				message => true,
				_RemoveSubscription);

			var broadcastBlock = _CreateBroadcastBlock();
			subscription.Link(broadcastBlock);

			// post
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestInformation());

			Helper.WaitForCondition(5000, 100, () => mSubscriberCallbackCalled == 2);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(2));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(0));
		}

		[Test]
		public void Test_MessageFilter2()
		{
			_Reset();

			// setup
			var subscription = new CallbackSubscription(
				_SubscriberCallback,
				typeof(TestInformation),
				message => true,
				_RemoveSubscription);

			var broadcastBlock = _CreateBroadcastBlock();
			subscription.Link(broadcastBlock);

			// post
			broadcastBlock.Post(new TestMessage());
			broadcastBlock.Post(new TestInformation());

			Helper.WaitForCondition(2000, 100, () => mSubscriberCallbackCalled == 2);
			Assert.That(mSubscriberCallbackCalled, Is.EqualTo(1));
			Assert.That(mRemoveSubscriptionCalled, Is.EqualTo(0));
		}

	}
}

#endif
